package com.dbuil.bleshoes.ui.activity;

import android.location.Location;
import android.os.Handler;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.dbuil.bleshoes.R;
import com.dbuil.bleshoes.base.BaseActivity;
import com.dbuil.bleshoes.utils.GPSUtils;

import butterknife.BindView;


public class GpsActivity extends BaseActivity {
    @BindView(R.id.tv_location)
    TextView tvLocation;
    private GPSUtils gpsUtils;
    private Location location;

    Handler handler = new Handler();
    @Override
    public int getLayoutId() {
        return R.layout.activity_gps;
    }

    @Override
    public void initData() {
        super.initData();

        gpsUtils = new GPSUtils(this);
        handler.postDelayed(runnable, 0);
    }


    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            location = gpsUtils.getLocation();//获取位置信息

            if (location != null) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        updateView(location);
                    }
                });
                //handler.removeCallbacks(runnable);
                handler.postDelayed(this, 1000);
            } else {
                handler.postDelayed(this, 1000);
            }
        }
    };


    /**
     * 实时更新文本内容
     *
     * @param location
     */
    private void updateView(Location location) {
        if (location != null) {
            tvLocation.setText("定位成功\n\n设备位置信息\n\n经度：");
            tvLocation.append(String.valueOf(location.getLongitude()));
            tvLocation.append("\n纬度：");
            tvLocation.append(String.valueOf(location.getLatitude()));
            tvLocation.append("\n海拔高度：");
            tvLocation.append(String.valueOf(location.getAltitude()));
            tvLocation.append("\n所在地址：");
            tvLocation.append(String.valueOf(gpsUtils.getCount()+"颗卫星")+"\n"+gpsUtils.getSnr());
        } else {
            // 清空EditText对象
            tvLocation.getEditableText().clear();
        }
    }

}
